package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Types;
import cn.wolfcode.crm.service.ITypesService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("types")
@Controller
public class TypesController {
    @Autowired
    private ITypesService typesService;

    @RequestMapping("")
    public String index(){

        System.out.println("=======================================");
        return "types/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public List<Types> list(){
        return typesService.getAll();
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            typesService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            new JsonResult("删除操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Types types){
        JsonResult result = new JsonResult();
        try {
            typesService.update(types);
        }catch (Exception e){
            e.printStackTrace();
            new JsonResult("更新操作失败");
        }
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Types types){
        JsonResult result = new JsonResult();
        try {
            typesService.save(types);

        }catch (Exception e){
            e.printStackTrace();
            new JsonResult("保存操作失败");
        }
        return result;
    }
}
